package listener;

import gui.ChessGUI;
import gui.PieceGUI;
import javafx.event.EventHandler;
import javafx.scene.input.DragEvent;
import javafx.scene.input.MouseEvent;
import logic.ChessGame;
import logic.Move;

public class PieceHandlerMouseDropped implements EventHandler<MouseEvent> {

	private ChessGUI chessGUI;
	private PieceGUI pieceGUI;

	public PieceHandlerMouseDropped(ChessGUI chessGUI, PieceGUI piece) {
		this.chessGUI = chessGUI;
		this.pieceGUI = piece;
	}

	@Override
	public void handle(MouseEvent event) {
		// pieceGUI.setLayoutX(ox);
		// pieceGUI.setLayoutY(oy);
		boolean isValidMove = chessGUI
				.getChessGame()
				.getMoveValidator()
				.isMoveValid(
						new Move(pieceGUI.getPiece().getRow(), pieceGUI
								.getPiece().getColumn(), pieceGUI.getY(),
								pieceGUI.getX()), true);
		if (isValidMove) {
			System.out.println("valid move");
			chessGUI.setCurrentMove(new Move(pieceGUI.getPiece().getRow(),
					pieceGUI.getPiece().getColumn(), pieceGUI.getY(), pieceGUI
							.getX()));
			System.out.println(chessGUI.getCurrentMove());
			// pieceGUI.setX(pieceGUI.getPiece().getColumn());
			// pieceGUI.setY(pieceGUI.getPiece().getRow());
			// pieceGUI.getPiece().setColumn(pieceGUI.getX());
			// pieceGUI.getPiece().setRow(pieceGUI.getY());

		} else {
			System.out.println("not a valid move");
			pieceGUI.setX(pieceGUI.getPiece().getColumn());
			pieceGUI.setY(pieceGUI.getPiece().getRow());
			chessGUI.resetPieces();
		}
		chessGUI.resetPiecesByCoor();
		chessGUI.removeAllMarkPoints();
		System.out.println("dropped");
	}
}
